# Copyright (c) 2024 Nordic Semiconductor ASA
# SPDX-License-Identifier: Apache-2.0

add_library(nrf-wifi-osal STATIC "")

# Automatically set CPU architecture and other flags from Zephyr interface
target_compile_options(
  nrf-wifi-osal
  PRIVATE
  $<TARGET_PROPERTY:zephyr_interface,INTERFACE_COMPILE_OPTIONS>
)
target_compile_definitions(
  nrf-wifi-osal
  PRIVATE
  $<TARGET_PROPERTY:zephyr_interface,INTERFACE_COMPILE_DEFINITIONS>
)
target_include_directories(
  nrf-wifi-osal
  PRIVATE
  $<TARGET_PROPERTY:zephyr_interface,INTERFACE_INCLUDE_DIRECTORIES>
)

set(NRF_WIFI_DIR ${ZEPHYR_NRF_WIFI_MODULE_DIR})

# Translate the configuration to the OS agnostic code
target_compile_definitions(
  nrf-wifi-osal
  PUBLIC
  $<$<BOOL:${CONFIG_NRF_WIFI_LOW_POWER}>:NRF_WIFI_LOW_POWER>
  $<$<BOOL:${CONFIG_NRF_WIFI_RPU_RECOVERY}>:NRF_WIFI_RPU_RECOVERY>
  $<$<BOOL:${CONFIG_NRF_WIFI_RPU_RECOVERY_DEBUG}>:NRF_WIFI_RPU_RECOVERY_DEBUG>
  $<$<BOOL:${CONFIG_NRF_WIFI_RPU_RECOVERY_PS_STATE_DEBUG}>:NRF_WIFI_RPU_RECOVERY_PS_STATE_DEBUG>
  $<$<BOOL:${CONFIG_NRF_WIFI_AP_DEAD_DETECT_TIMEOUT}>:NRF_WIFI_AP_DEAD_DETECT_TIMEOUT=${CONFIG_NRF_WIFI_AP_DEAD_DETECT_TIMEOUT}>
  $<$<BOOL:${CONFIG_NRF_WIFI_IFACE_MTU}>:NRF_WIFI_IFACE_MTU=${CONFIG_NRF_WIFI_IFACE_MTU}>
  $<$<BOOL:${CONFIG_NRF70_STA_MODE}>:NRF70_STA_MODE>
  $<$<BOOL:${CONFIG_NRF70_DATA_TX}>:NRF70_DATA_TX>
  $<$<BOOL:${CONFIG_NRF70_RAW_DATA_TX}>:NRF70_RAW_DATA_TX>
  $<$<BOOL:${CONFIG_NRF70_RAW_DATA_RX}>:NRF70_RAW_DATA_RX>
  $<$<BOOL:${CONFIG_NRF70_PROMISC_DATA_RX}>:NRF70_PROMISC_DATA_RX>
  $<$<BOOL:${CONFIG_NRF70_TX_DONE_WQ_ENABLED}>:NRF70_TX_DONE_WQ_ENABLED>
  $<$<BOOL:${CONFIG_NRF70_RX_WQ_ENABLED}>:NRF70_RX_WQ_ENABLED>
  $<$<BOOL:${CONFIG_NRF70_UTIL}>:NRF70_UTIL>
  $<$<OR:$<BOOL:${CONFIG_NRF70_RADIO_TEST}>,$<BOOL:${CONFIG_NRF70_BM_RADIO_TEST}>>:NRF70_RADIO_TEST>
  $<$<BOOL:${CONFIG_NRF70_OFFLOADED_RAW_TX}>:NRF70_OFFLOADED_RAW_TX>
  $<$<BOOL:${CONFIG_NRF70_TCP_IP_CHECKSUM_OFFLOAD}>:NRF70_TCP_IP_CHECKSUM_OFFLOAD>
  $<$<BOOL:${CONFIG_NRF70_RPU_EXTEND_TWT_SP}>:NRF70_RPU_EXTEND_TWT_SP>
  $<$<BOOL:${CONFIG_NRF70_SYSTEM_WITH_RAW_MODES}>:NRF70_SYSTEM_WITH_RAW_MODES>
  $<$<OR:$<BOOL:${CONFIG_NRF70_SCAN_ONLY}>,$<BOOL:${CONFIG_NRF70_BM_SCAN_ONLY}>>:NRF70_SCAN_ONLY>
  $<$<BOOL:${CONFIG_NRF70_SYSTEM_MODE}>:NRF70_SYSTEM_MODE>
  $<$<BOOL:${CONFIG_NRF70_2_4G_ONLY}>:NRF70_2_4G_ONLY>
  $<$<BOOL:${CONFIG_NRF70_LOG_VERBOSE}>:NRF70_LOG_VERBOSE>
  $<$<BOOL:${CONFIG_NRF70_AP_MODE}>:NRF70_AP_MODE>
  $<$<BOOL:${CONFIG_NRF_WIFI_MGMT_BUFF_OFFLOAD}>:NRF_WIFI_MGMT_BUFF_OFFLOAD>
  $<$<BOOL:${CONFIG_NRF_WIFI_FEAT_KEEPALIVE}>:NRF_WIFI_FEAT_KEEPALIVE>
  $<$<BOOL:${CONFIG_NRF_WIFI_FEAT_KEEPALIVE}>:NRF_WIFI_KEEPALIVE_PERIOD_S=${CONFIG_NRF_WIFI_KEEPALIVE_PERIOD_S}>
  $<$<BOOL:${CONFIG_WIFI_MGMT_RAW_SCAN_RESULTS}>:WIFI_MGMT_RAW_SCAN_RESULTS>
  $<$<BOOL:${CONFIG_NRF_WIFI_COEX_DISABLE_PRIORITY_WINDOW_FOR_SCAN}>:NRF_WIFI_COEX_DISABLE_PRIORITY_WINDOW_FOR_SCAN>
  $<$<BOOL:${CONFIG_NRF_WIFI_RX_STBC_HT}>:NRF_WIFI_RX_STBC_HT>
  $<$<BOOL:${CONFIG_NRF70_SR_COEX_SLEEP_CTRL_GPIO_CTRL}>:NRF70_SR_COEX_SLEEP_CTRL_GPIO_CTRL>
  $<$<BOOL:${CONFIG_NRF71_ON_IPC}>:NRF71_ON_IPC>
  $<$<BOOL:${CONFIG_NRF_WIFI_DYNAMIC_BANDWIDTH_SIGNALLING}>:NRF_WIFI_DYNAMIC_BANDWIDTH_SIGNALLING>
  $<$<BOOL:${CONFIG_NRF_WIFI_DYNAMIC_ED}>:NRF_WIFI_DYNAMIC_ED>
  NRF_WIFI_MAX_PS_POLL_FAIL_CNT=${CONFIG_NRF_WIFI_MAX_PS_POLL_FAIL_CNT}
  NRF70_RX_NUM_BUFS=${CONFIG_NRF70_RX_NUM_BUFS}
  NRF70_MAX_TX_TOKENS=${CONFIG_NRF70_MAX_TX_TOKENS}
  NRF70_RX_MAX_DATA_SIZE=${CONFIG_NRF70_RX_MAX_DATA_SIZE}
  NRF70_MAX_TX_PENDING_QLEN=${CONFIG_NRF70_MAX_TX_PENDING_QLEN}
  NRF70_RPU_PS_IDLE_TIMEOUT_MS=${CONFIG_NRF70_RPU_PS_IDLE_TIMEOUT_MS}
  NRF70_BAND_2G_LOWER_EDGE_BACKOFF_DSSS=${CONFIG_NRF70_BAND_2G_LOWER_EDGE_BACKOFF_DSSS}
  NRF70_BAND_2G_LOWER_EDGE_BACKOFF_HT=${CONFIG_NRF70_BAND_2G_LOWER_EDGE_BACKOFF_HT}
  NRF70_BAND_2G_LOWER_EDGE_BACKOFF_HE=${CONFIG_NRF70_BAND_2G_LOWER_EDGE_BACKOFF_HE}
  NRF70_BAND_2G_UPPER_EDGE_BACKOFF_DSSS=${CONFIG_NRF70_BAND_2G_UPPER_EDGE_BACKOFF_DSSS}
  NRF70_BAND_2G_UPPER_EDGE_BACKOFF_HT=${CONFIG_NRF70_BAND_2G_UPPER_EDGE_BACKOFF_HT}
  NRF70_BAND_2G_UPPER_EDGE_BACKOFF_HE=${CONFIG_NRF70_BAND_2G_UPPER_EDGE_BACKOFF_HE}
  NRF70_BAND_UNII_1_LOWER_EDGE_BACKOFF_HT=${CONFIG_NRF70_BAND_UNII_1_LOWER_EDGE_BACKOFF_HT}
  NRF70_BAND_UNII_1_LOWER_EDGE_BACKOFF_HE=${CONFIG_NRF70_BAND_UNII_1_LOWER_EDGE_BACKOFF_HE}
  NRF70_BAND_UNII_1_UPPER_EDGE_BACKOFF_HT=${CONFIG_NRF70_BAND_UNII_1_UPPER_EDGE_BACKOFF_HT}
  NRF70_BAND_UNII_1_UPPER_EDGE_BACKOFF_HE=${CONFIG_NRF70_BAND_UNII_1_UPPER_EDGE_BACKOFF_HE}
  NRF70_BAND_UNII_2A_LOWER_EDGE_BACKOFF_HT=${CONFIG_NRF70_BAND_UNII_2A_LOWER_EDGE_BACKOFF_HT}
  NRF70_BAND_UNII_2A_LOWER_EDGE_BACKOFF_HE=${CONFIG_NRF70_BAND_UNII_2A_LOWER_EDGE_BACKOFF_HE}
  NRF70_BAND_UNII_2A_UPPER_EDGE_BACKOFF_HT=${CONFIG_NRF70_BAND_UNII_2A_UPPER_EDGE_BACKOFF_HT}
  NRF70_BAND_UNII_2A_UPPER_EDGE_BACKOFF_HE=${CONFIG_NRF70_BAND_UNII_2A_UPPER_EDGE_BACKOFF_HE}
  NRF70_BAND_UNII_2C_LOWER_EDGE_BACKOFF_HT=${CONFIG_NRF70_BAND_UNII_2C_LOWER_EDGE_BACKOFF_HT}
  NRF70_BAND_UNII_2C_LOWER_EDGE_BACKOFF_HE=${CONFIG_NRF70_BAND_UNII_2C_LOWER_EDGE_BACKOFF_HE}
  NRF70_BAND_UNII_2C_UPPER_EDGE_BACKOFF_HT=${CONFIG_NRF70_BAND_UNII_2C_UPPER_EDGE_BACKOFF_HT}
  NRF70_BAND_UNII_2C_UPPER_EDGE_BACKOFF_HE=${CONFIG_NRF70_BAND_UNII_2C_UPPER_EDGE_BACKOFF_HE}
  NRF70_BAND_UNII_3_LOWER_EDGE_BACKOFF_HT=${CONFIG_NRF70_BAND_UNII_3_LOWER_EDGE_BACKOFF_HT}
  NRF70_BAND_UNII_3_LOWER_EDGE_BACKOFF_HE=${CONFIG_NRF70_BAND_UNII_3_LOWER_EDGE_BACKOFF_HE}
  NRF70_BAND_UNII_3_UPPER_EDGE_BACKOFF_HT=${CONFIG_NRF70_BAND_UNII_3_UPPER_EDGE_BACKOFF_HT}
  NRF70_BAND_UNII_3_UPPER_EDGE_BACKOFF_HE=${CONFIG_NRF70_BAND_UNII_3_UPPER_EDGE_BACKOFF_HE}
  NRF70_BAND_UNII_4_LOWER_EDGE_BACKOFF_HT=${CONFIG_NRF70_BAND_UNII_4_LOWER_EDGE_BACKOFF_HT}
  NRF70_BAND_UNII_4_LOWER_EDGE_BACKOFF_HE=${CONFIG_NRF70_BAND_UNII_4_LOWER_EDGE_BACKOFF_HE}
  NRF70_BAND_UNII_4_UPPER_EDGE_BACKOFF_HT=${CONFIG_NRF70_BAND_UNII_4_UPPER_EDGE_BACKOFF_HT}
  NRF70_BAND_UNII_4_UPPER_EDGE_BACKOFF_HE=${CONFIG_NRF70_BAND_UNII_4_UPPER_EDGE_BACKOFF_HE}
  NRF70_PCB_LOSS_2G=${CONFIG_NRF70_PCB_LOSS_2G}
  NRF70_PCB_LOSS_5G_BAND1=${CONFIG_NRF70_PCB_LOSS_5G_BAND1}
  NRF70_PCB_LOSS_5G_BAND2=${CONFIG_NRF70_PCB_LOSS_5G_BAND2}
  NRF70_PCB_LOSS_5G_BAND3=${CONFIG_NRF70_PCB_LOSS_5G_BAND3}
  NRF70_ANT_GAIN_2G=${CONFIG_NRF70_ANT_GAIN_2G}
  NRF70_ANT_GAIN_5G_BAND1=${CONFIG_NRF70_ANT_GAIN_5G_BAND1}
  NRF70_ANT_GAIN_5G_BAND2=${CONFIG_NRF70_ANT_GAIN_5G_BAND2}
  NRF70_ANT_GAIN_5G_BAND3=${CONFIG_NRF70_ANT_GAIN_5G_BAND3}
  NRF_WIFI_PS_INT_PS=${CONFIG_NRF_WIFI_PS_INT_PS}
  NRF_WIFI_RPU_RECOVERY_PS_ACTIVE_TIMEOUT_MS=${CONFIG_NRF_WIFI_RPU_RECOVERY_PS_ACTIVE_TIMEOUT_MS}
  NRF_WIFI_DISPLAY_SCAN_BSS_LIMIT=${CONFIG_NRF_WIFI_DISPLAY_SCAN_BSS_LIMIT}
  NRF_WIFI_RPU_MIN_TIME_TO_ENTER_SLEEP_MS=${CONFIG_NRF_WIFI_RPU_MIN_TIME_TO_ENTER_SLEEP_MS}
  WIFI_NRF70_LOG_LEVEL=${CONFIG_WIFI_NRF70_LOG_LEVEL}
)

target_include_directories(
  nrf-wifi-osal
  PUBLIC
  ${CMAKE_CURRENT_LIST_DIR}
  ${CMAKE_CURRENT_SOURCE_DIR}/../bus
  ${NRF_WIFI_DIR}/utils/inc
  ${NRF_WIFI_DIR}/os_if/inc
  ${NRF_WIFI_DIR}/bus_if/bus/qspi/inc
  ${NRF_WIFI_DIR}/bus_if/bal/inc
  ${NRF_WIFI_DIR}/fw_if/umac_if/inc
  ${NRF_WIFI_DIR}/fw_load/mips/fw/inc
  ${NRF_WIFI_DIR}/hw_if/hal/inc
  ${NRF_WIFI_DIR}/fw_if/umac_if/inc/fw
  ${NRF_WIFI_DIR}/fw_if/umac_if/inc/fw/stats
)

target_include_directories_ifdef(CONFIG_NRF70_SYSTEM_MODE
  nrf-wifi-osal
  PUBLIC
  ${NRF_WIFI_DIR}/fw_if/umac_if/inc/fw/stats/system
)

target_include_directories_ifdef(CONFIG_NRF70_RADIO_TEST
  nrf-wifi-osal
  PUBLIC
  ${NRF_WIFI_DIR}/fw_if/umac_if/inc/fw/stats/radio_test
)

target_include_directories_ifdef(CONFIG_NRF70_OFFLOADED_RAW_TX
  nrf-wifi-osal
  PUBLIC
  ${NRF_WIFI_DIR}/fw_if/umac_if/inc/fw/stats/offload_raw_tx
)

target_include_directories_ifdef(CONFIG_NRF70_SCAN_ONLY
  nrf-wifi-osal
  PUBLIC
  ${NRF_WIFI_DIR}/fw_if/umac_if/inc/fw/stats/scan_only
)

target_include_directories_ifdef(CONFIG_NRF70_SYSTEM_MODE
  nrf-wifi-osal
  PUBLIC
  ${NRF_WIFI_DIR}/fw_if/umac_if/inc/fw/stats/system
)

target_sources(nrf-wifi-osal PRIVATE
  ${NRF_WIFI_DIR}/os_if/src/osal.c
  ${NRF_WIFI_DIR}/utils/src/list.c
  ${NRF_WIFI_DIR}/utils/src/queue.c
  ${NRF_WIFI_DIR}/utils/src/util.c
  ${NRF_WIFI_DIR}/hw_if/hal/src/common/hal_api_common.c
  ${NRF_WIFI_DIR}/hw_if/hal/src/common/hal_fw_patch_loader.c
  ${NRF_WIFI_DIR}/bus_if/bal/src/bal.c
  ${NRF_WIFI_DIR}/bus_if/bus/qspi/src/qspi.c
  ${NRF_WIFI_DIR}/fw_if/umac_if/src/common/fmac_cmd_common.c
  ${NRF_WIFI_DIR}/fw_if/umac_if/src/common/fmac_api_common.c
  ${NRF_WIFI_DIR}/fw_if/umac_if/src/common/fmac_util.c
)

if(NOT CONFIG_NRF71_ON_IPC)
  target_sources(nrf-wifi-osal PRIVATE
    ${NRF_WIFI_DIR}/hw_if/hal/src/common/hal_interrupt.c
    ${NRF_WIFI_DIR}/hw_if/hal/src/common/hal_mem.c
    ${NRF_WIFI_DIR}/hw_if/hal/src/common/hal_reg.c
    ${NRF_WIFI_DIR}/hw_if/hal/src/common/hpqm.c
    ${NRF_WIFI_DIR}/hw_if/hal/src/common/pal.c
  )
endif()

if(CONFIG_NRF70_RADIO_TEST OR CONFIG_NRF70_BM_RADIO_TEST)
  target_sources(nrf-wifi-osal PRIVATE
    ${NRF_WIFI_DIR}/fw_if/umac_if/src/radio_test/fmac_api.c
    ${NRF_WIFI_DIR}/fw_if/umac_if/src/radio_test/fmac_cmd.c
    ${NRF_WIFI_DIR}/fw_if/umac_if/src/radio_test/fmac_event.c
    ${NRF_WIFI_DIR}/hw_if/hal/src/radio_test/hal_api.c
  )
endif ()

if(CONFIG_NRF70_OFFLOADED_RAW_TX)
  target_sources(nrf-wifi-osal PRIVATE
    ${NRF_WIFI_DIR}/fw_if/umac_if/src/offload_raw_tx/fmac_api.c
    ${NRF_WIFI_DIR}/fw_if/umac_if/src/offload_raw_tx/fmac_cmd.c
    ${NRF_WIFI_DIR}/fw_if/umac_if/src/offload_raw_tx/fmac_event.c
    ${NRF_WIFI_DIR}/hw_if/hal/src/offload_raw_tx/hal_api.c
  )
endif()

if(CONFIG_NRF70_SYSTEM_MODE OR CONFIG_NRF70_SCAN_ONLY OR CONFIG_NRF70_BM_SCAN_ONLY)
  target_sources(nrf-wifi-osal PRIVATE
    ${NRF_WIFI_DIR}/fw_if/umac_if/src/system/rx.c
    ${NRF_WIFI_DIR}/fw_if/umac_if/src/system/fmac_vif.c
    ${NRF_WIFI_DIR}/fw_if/umac_if/src/system/fmac_api.c
    ${NRF_WIFI_DIR}/fw_if/umac_if/src/system/fmac_cmd.c
    ${NRF_WIFI_DIR}/fw_if/umac_if/src/system/fmac_event.c
    ${NRF_WIFI_DIR}/hw_if/hal/src/system/hal_api.c
    $<$<BOOL:${CONFIG_NRF70_DATA_TX}>:${NRF_WIFI_DIR}/fw_if/umac_if/src/system/tx.c>
    $<$<BOOL:${CONFIG_NRF70_DATA_TX}>:${NRF_WIFI_DIR}/fw_if/umac_if/src/system/fmac_peer.c>
    $<$<BOOL:${CONFIG_NRF70_STA_MODE}>:${NRF_WIFI_DIR}/fw_if/umac_if/src/system/fmac_peer.c>
    $<$<BOOL:${CONFIG_NRF70_PROMISC_DATA_RX}>:${NRF_WIFI_DIR}/fw_if/umac_if/src/system/fmac_promisc.c>
    $<$<BOOL:${CONFIG_NRF70_AP_MODE}>:${NRF_WIFI_DIR}/fw_if/umac_if/src/system/fmac_ap.c>
  )
endif()
